package com.imy.common.util;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by user on 2016/11/22.
 */

public class UiUtil {
    /**
     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }
    public static SpannableStringBuilder changeTextPartSize(String context, int size, String needBig) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(context);
        int start = context.indexOf(needBig);
        int end = start + needBig.length();
        stylebuilder.setSpan(new AbsoluteSizeSpan(size), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return stylebuilder;
    }

    public static SpannableStringBuilder changeTextPartSize(CharSequence context, int size, String needBig) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(context);
        int start = context.toString().indexOf(needBig);
        int end = start + needBig.length();
        stylebuilder.setSpan(new AbsoluteSizeSpan(size), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return stylebuilder;
    }

    public static SpannableStringBuilder changeTextPartSize(String context, int size, String needBig,
                                                            String need_red) {
        try {
            SpannableStringBuilder stylebuilder = new SpannableStringBuilder(context);
            int big_start = context.indexOf(needBig);
            int big_end = big_start + needBig.length();
            stylebuilder.setSpan(new AbsoluteSizeSpan(size), big_start, big_end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor("#fc4b4e"));
            int red_start = context.indexOf(need_red);
            int red_end = red_start + need_red.length();
            stylebuilder.setSpan(redSpan, red_start, red_end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            return stylebuilder;
        } catch (Exception e) {
            return new SpannableStringBuilder(context);
        }


    }

    public static SpannableStringBuilder changeTextPartSize(CharSequence context, int size, String needBig,
                                                            String need_red, int color) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(context);
        int big_start = context.toString().indexOf(needBig);
        int big_end = big_start + needBig.length();
        stylebuilder.setSpan(new AbsoluteSizeSpan(size), big_start, big_end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        int red_start = context.toString().indexOf(need_red);
        int red_end = red_start + need_red.length();
        stylebuilder.setSpan(redSpan, red_start, red_end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return stylebuilder;
    }

    public static SpannableStringBuilder changeTextColor(String text, int color, String need_red, boolean needBold) {
        try {
            SpannableStringBuilder stylebuilder = new SpannableStringBuilder(text);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
            int start = text.indexOf(need_red);

            int end = start + need_red.length();
            stylebuilder.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            if (needBold) {
                stylebuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                stylebuilder.setSpan(new StyleSpan(Typeface.NORMAL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return stylebuilder;
        } catch (Exception e) {
            return new SpannableStringBuilder(text);
        }
    }

    public static SpannableStringBuilder changeTextColor(String text, int color, String need_red, boolean needBold,
                                                         boolean use_last_one) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(text);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        int start;
        int end;
        if (use_last_one) {
            start = text.lastIndexOf(need_red);
            end = start + need_red.length();
        } else {
            start = text.indexOf(need_red);
            end = start + need_red.length();
        }
        stylebuilder.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        if (needBold) {
            stylebuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            stylebuilder.setSpan(new StyleSpan(Typeface.NORMAL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return stylebuilder;
    }


    public static SpannableStringBuilder changeTextBold(CharSequence text, String need_bold, boolean needBold) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(text);
        int start = text.toString().indexOf(need_bold);
        int end = start + need_bold.length();
        if (needBold) {
            stylebuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            stylebuilder.setSpan(new StyleSpan(Typeface.NORMAL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return stylebuilder;
    }

    public static SpannableStringBuilder changeTextColor(CharSequence text, int color, String need_red, boolean needBold) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(text);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        int start = text.toString().indexOf(need_red);
        int end = start + need_red.length();
        stylebuilder.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        if (needBold) {
            stylebuilder.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            stylebuilder.setSpan(new StyleSpan(Typeface.NORMAL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return stylebuilder;
    }

    public static SpannableStringBuilder changeTextColor(String text, int color, List<String> need_red) {
        SpannableStringBuilder stylebuilder = new SpannableStringBuilder(text);
        int last_start = 0;
        for (int i = 0; i < need_red.size(); i++) {
            int start = text.indexOf(need_red.get(i));
            int end = start + need_red.get(i).length();
            if (last_start == start ) {
                start = text.indexOf(need_red.get(i), start + 1);
                end = start + need_red.get(i).length();
            }
            last_start = start;

            ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
            stylebuilder.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }
        return stylebuilder;
    }

    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { // 如果不能匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * dp转px
     */
    public static int dp2Px(Context context,float dp) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static boolean containsSymbol(String name) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？ —" +
                "…–．《》〈〉]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(name);
        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }

    public static void copy(String content, Context context) {
// 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
    }

    public static boolean isCapitalLetters(String str) {
        Pattern pattern = Pattern.compile("[A-Z]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
                || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    public static String ignoreLineFeed(String text) {
        String re = text.replaceAll(" ", "");
        return re;
    }

    public static void setTextLeftDrawable(Context mContext, TextView textView, int icon_res_id) {
        Drawable drawable = null;
        if (icon_res_id != 0) {
            drawable = mContext.getResources().getDrawable(icon_res_id);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    public static void setTextRightDrawable(Context mContext, TextView textView, int icon_res_id){
        Drawable drawable = null;
        if (icon_res_id != 0) {
            drawable = mContext.getResources().getDrawable(icon_res_id);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        textView.setCompoundDrawables(null, null, drawable, null);
    }
    public static void clearTextDrawable(TextView textView) {
        textView.setCompoundDrawables(null, null, null, null);
    }




    public static int[] getDeviceHW(Context context) {
        int[] ints = new int[2];
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        if (windowManager != null) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
            ints[0] = displayMetrics.widthPixels;
            ints[1] = displayMetrics.heightPixels;
            return ints;
        } else {
            ints[0] = getDeviceWidth(context);
            ints[1] = getDeviceHeight(context);
            return ints;
        }

    }

    public static int getDeviceHeight(Context context) {
        Resources resources = context.getResources();
        return resources.getDisplayMetrics().heightPixels;
    }

    public static int getDeviceWidth(Context context) {
        Resources resources = context.getResources();
        return resources.getDisplayMetrics().widthPixels;
    }

    /**
     * dp转px
     */
    public static int dp2Px(float dp, Context context) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
